package simonxianyu.learn.springmybatis.web;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import simonxianyu.learn.springmybatis.db.dbsvc.DeviceDbSvc;
import simonxianyu.learn.springmybatis.db.po.Device;
import simonxianyu.learn.springmybatis.util.DataTableInput;
import simonxianyu.learn.springmybatis.util.DataTableResult;

import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Objects;

/**
 */
@Controller
@RequestMapping("/device")
public class DeviceCtl extends BaseCtl {

  @Autowired
  private DeviceDbSvc dbSvc;

  @RequestMapping("/aj_grid")
  @ResponseBody
  public DataTableResult<?> grid(HttpSession session, DataTableInput dti) {
    String partnerId = Objects.toString( session.getAttribute(PARTNER_NO));
    int start = dti.getStart();
    int size = dti.getLength();
    long count = dbSvc.countAll(partnerId);
    List<Device> devices = dbSvc.findAllPaged(partnerId, start, size);
    return new DataTableResult<>(dti.getDraw(), count, devices.size(), devices);
  }
}
